package ua.kharkov.knure.dereza.summarytask4.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.core.Constants;
import ua.kharkov.knure.dereza.summarytask4.db.bean.ClientBean;

/**
 * No command
 * 
 * @author dereza
 * 
 */
public class NoCommand extends Command {

	private static final long serialVersionUID = 2591150483854314680L;

	private static final Logger log = Logger.getLogger(NoCommand.class);

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res) {
		log.debug("Command started");

		// gets client's session
		HttpSession session = req.getSession(false);

		// gets ClientBean from session
		ClientBean clientBean = (ClientBean) session.getAttribute("clientbean");
		log.trace("Got clientbean  from session --> " + clientBean);

		// if clientBean == null forward to login page
		if (clientBean == null) {
			String forward = Constants.COMMAND_LOGOUT;
			return forward;
		}

		String errorMessage = "No such command";
		String forward = Constants.PAGE_ERROR_PAGE;

		req.setAttribute("errorMessage", errorMessage);
		log.error("Set the request attribute: errorMessage --> " + errorMessage);

		log.debug("Command finished");
		return forward;
	}

	@Override
	public String toString() {
		return "NoCommand";
	}
}
